/**
 * 
 */
package ru.olympus.antonyhands.ao.client.widgets;

import java.util.ArrayList;
import java.util.List;

import ru.olympus.antonyhands.ao.client.AOHelperServiceAsync;
import ru.olympus.antonyhands.ao.shared.item.CraftInfoDTO;
import ru.olympus.antonyhands.ao.shared.item.EItemParentType;
import ru.olympus.antonyhands.ao.shared.item.EItemsViewType;
import ru.olympus.antonyhands.ao.shared.util.ItemQuantityDTO;
import ru.olympus.antonyhands.ao.shared.util.StringPair;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author AntonyHands
 * @since 28.02.2012
 */
public class ItemCraftTable2 extends Grid
{
    private AOHelperServiceAsync _service;

    private TextBox craftLevel;
    private TextBox workload;
    private TextBox createdElementsNumber;
    private FlexTable resourcesTable;
    private Button addButton;

    private boolean _editable = true;

    public ItemCraftTable2(AOHelperServiceAsync service)
    {
        super(5, 2);
        _service = service;

        setHTML(0, 0, "Уровень навыка");
        setHTML(1, 0, "Количество единиц труда");
        setHTML(2, 0, "Количество произведённых элементов");
        setHTML(3, 0, "Ресурсы");

        craftLevel = new TextBox();
        setWidget(0, 1, craftLevel);

        workload = new TextBox();
        setWidget(1, 1, workload);

        createdElementsNumber = new TextBox();
        setWidget(2, 1, createdElementsNumber);

        resourcesTable = new FlexTable();
        setWidget(3, 1, resourcesTable);

        addButton = new Button("Добавить");
        setWidget(4, 1, addButton);
        addButton.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
            	addTableRow(null);
            }
        });
    }

    private void addTableRow(ItemQuantityDTO info)
    {
    	final int rows = resourcesTable.getRowCount();
        final ItemCategoriesListBox __category = new ItemCategoriesListBox(_service, info == null ? null : info.getCategoryCode());
        resourcesTable.setWidget(rows, 0, __category);
        resourcesTable.setWidget(rows, 1, new ItemsListBox(_service, __category, EItemParentType.ITEM_CATEGORY, EItemsViewType.CRAFTABLE, info == null ? null : info.getCode()));
        TextBox tb = new TextBox();
        resourcesTable.setWidget(rows, 2, tb);
        if (info != null)
        	tb.setText(info.getQuantity());
        final Button __delButton = new Button("Del");
        resourcesTable.setWidget(rows, 3, __delButton);
        __delButton.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                for (int i = 0; i < resourcesTable.getRowCount(); i++)
                    if (__delButton.equals(resourcesTable.getWidget(i, 3)))
                    {
                        resourcesTable.removeRow(i);
                        break;
                    }
            }
        });
        setEditable(_editable);
    }

    public void setEditable(boolean enabled)
    {
    	_editable = enabled;
    	craftLevel.setEnabled(_editable);
        workload.setEnabled(_editable);
        createdElementsNumber.setEnabled(_editable);
        for (int i = 0; i < resourcesTable.getRowCount(); i++)
        {
        	((ItemCategoriesListBox)resourcesTable.getWidget(i, 0)).setEnabled(_editable);
        	((ItemsListBox)resourcesTable.getWidget(i, 1)).setEnabled(_editable);
        	((TextBox)resourcesTable.getWidget(i, 2)).setEnabled(_editable);
        	((Button)resourcesTable.getWidget(i, 3)).setVisible(_editable);
        }
        addButton.setVisible(_editable);
    }

    public void setDefaultValues(CraftInfoDTO info)
    {
    	craftLevel.setText(info.getCraftLevel());
    	workload.setText(info.getWorkload());
    	createdElementsNumber.setText(info.getResultItem().getQuantity());
    	resourcesTable.removeAllRows();
    	for (ItemQuantityDTO iqDTO : info.getInputItems())
    	{
    		addTableRow(iqDTO);
    	}
    }

    public String getCraftLevel()
    {
        return craftLevel.getText();
    }

    public String getWorkload()
    {
        return workload.getText();
    }
    
    public String getCraftQuantity()
    {
        return createdElementsNumber.getText();
    }
    
    public List<StringPair> listResources()
    {
        List<StringPair> res = new ArrayList<StringPair>();
        for (int i = 0; i < resourcesTable.getRowCount(); i++)
        {
            res.add(new StringPair(
                    ((ItemsListBox)resourcesTable.getWidget(i, 1)).getCurrentValueCode(),
                    ((TextBox)resourcesTable.getWidget(i, 2)).getText()));
        }
        return res;
    }
    
}
